<?php 

// src/Acme/StoreBundle/Controller/RandomController.php
namespace Acme\StoreBundle\Controller;
use Acme\StoreBundle\Entity\Comment;
use Symfony\Component\HttpFoundation\Request;
use Acme\StoreBundle\Helper\BaseController;

class HomeController extends BaseController {
    private $_limit = 2;
    
    public function indexAction() {
        
        $posts = $this->getPostRepository()
                ->findBy(array('is_display' => 1), array('id' => 'DESC'));
        
        return $this->render(
            'StoreBundle:Home:index.html.twig',
            array ('posts' => $posts)
        );
    }
    
    public function detailAction( $id ) {
        $post = $this->getDoctrine()->getRepository('StoreBundle:Post')
                ->find($id);
        
        if ( !$post ) {
            return $this->createNotFoundException('Not found post');
        }
        
        $totalComments = count($this->getCommentsQueryBuilder($id)->getQuery()->getResult());
        $hasMoreComments = $totalComments > $this->_limit;
        
        return $this->render(
            'StoreBundle:Home:detail.html.twig', array (
                'post'              => $post,
                'hasMoreComments'    => $hasMoreComments
            )
        );
    }

    public function getCommentsQueryBuilder( $postId ) {
        
        $repository = $this->getDoctrine()->getRepository('StoreBundle:Comment');
        return $repository->createQueryBuilder('c')
                    ->where('c.post = :postId')
                    ->orderBy('c.id', 'DESC')
                    ->setParameter('postId', $postId);
    }


    /**
     * Render comments html for post
     * @return string html
     */
    public function getCommentsAction( $postId ) {
       
        $commentQueryBuilder = $this->getCommentsQueryBuilder( $postId );
        $query = $commentQueryBuilder->setMaxResults( $this->_limit )->getQuery();
        return $this->render(
            'StoreBundle:Home:comment-item.html.twig', array(
                'comments' => $query->getResult()
            )
        );
    }
    
    public function addCommentAction( Request $request ) {
        
        $postId = $request->request->get('post_id');
        $content = $request->request->get('content');
        
        $post = $this->getDoctrine()->getRepository('StoreBundle:Post')
                ->find($postId);
        
        if ( ! $post ) {
            exit(0);
        }
        $em = $this->getDoctrine()->getManager();
        $comment = new Comment();
        $comment->setPost($post);
        $comment->setContent($content);
        $em->persist($comment);
        $em->flush();
        
        exit(1);
     }
     
     public function moreCommentAction( Request $request ) {
         
        $offset = $request->request->get('offset');
        $postId = $request->request->get('post_id');
        $limit  = 2;

        $builderQuery = $this->getCommentsQueryBuilder($postId);

        $total = count( $builderQuery->getQuery()->getResult() );
        $query = $builderQuery->setFirstResult( $offset )
                              ->setMaxResults( $limit );

        $comments = $query->getQuery()->getResult();
        $hasMoreComments = $total > $offset + $limit;

        $html = $this->renderView('StoreBundle:Home:comment-item.html.twig', array (
            'comments'     => $comments,
        ));

        echo json_encode(array(
            'html'         => $html,
            'hasMoreComments' => $hasMoreComments
        ));
        exit;
     }
}